# -*-* encoding:UTF-8 -*-
# author : mengy
# date   : 2018/11/22

import Queue
import threading
import time

exitFlag = 0

class myThread(threading.Thread):
    def __init__(self,threadId,name,q):
        threading.Thread.__init__(self)
        self.threadId = threadId
        self.name = name
        self.q = q
    def run(self):
        print self.name,"线程开始"
        process_data(self.name,self.q)
        print self.name,"线程结束"

def process_data(threadName,q):
    while not exitFlag:
        queueLock.acquire()
        if not workQueue.empty():
            data = q.get()
            queueLock.release()
            print "%s 处理 %s" % (threadName,data)
        else:
            queueLock.release()
        time.sleep(1)

threadList = ["Thread-1","Thread-2","Thread-3","Thread-4","Thread-5"]
nameList = ["one","twe","three","four","five","six","seven","eight","nine","ten"]
queueLock = threading.Lock()
# workQueue = Queue.Queue(10)
workQueue = Queue.Queue(10)
threads = []
threadId = 1

# 创建新线程
for tName in threadList:
    thread = myThread(threadId,tName,workQueue)
    thread.start()
    threads.append(thread)
    threadId += 1

# 填充队列
queueLock.acquire()
for word in nameList:
    workQueue.put(word)
queueLock.release()

# 等待队列清空
while not workQueue.empty():
    pass

# 通知线程退出
exitFlag = 1

# 等待所有线程完成
for t in threads:
    t.join()

print "主线程结束！"

